package br.mikhas.task;

import static br.mikhas.task.Status.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.mikhas.task.metadata.User;

/**
 * A task definition
 * 
 * @author Mikhail Domanoski
 * 
 */
public class SimpleTaskBean implements Task {

	protected long number;

	/**
	 * Task's name/title
	 */
	protected String name;

	/**
	 * The user this task is assigned to
	 */
	protected User assignee;

	/**
	 * A cron-like expression which defines the task recurrence
	 */
	protected String recurrence;

	/**
	 * The task which generates this ocurrence
	 */
	protected Task parent;

	/**
	 * The date/time this task has been created
	 */
	protected Date creation;

	/**
	 * The date/time this task is scheduled to start
	 */
	protected Date start;

	/**
	 * The task duration in minuts.
	 * <p>
	 * The duration is used to calculate the due time from the start time
	 */
	protected int duration;

	/**
	 * The {@link TaskList} this task belongs
	 */
	protected TaskList list;

	/**
	 * Current task status
	 */
	protected Status status = Open;

	/**
	 * Holds the task history
	 */
	protected transient List<History> history;

	public SimpleTaskBean(Task task, Date start) {
		this.assignee = task.getAssignee();
		this.creation = new Date();
		this.duration = task.getDuration();
		this.list = task.getList();
		this.name = task.getName();
		this.parent = task;
		this.start = start;
		this.status = Open;
	}

	public SimpleTaskBean() {

	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the recurrent
	 */
	public boolean isRecurrent() {
		return recurrence != null;
	}

	public void setRecurrence(String expression) {
		this.recurrence = expression;
	}

	/**
	 * @return the start
	 */
	public Date getStart() {
		return start;
	}

	@Override
	public String toString() {
		return "Task[" + this.number + " - " + this.name + ']';
	}

	public void setStart(Date start) {
		this.start = start;
	}

	@Override
	public void setList(TaskList taskList) {
		this.list = taskList;
	}

	@Override
	public TaskList getList() {
		return this.list;
	}

	@Override
	public String getRecurrence() {
		return this.recurrence;
	}

	@Override
	public User getAssignee() {
		return this.assignee;
	}

	@Override
	public int getDuration() {
		return this.duration;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void addHistoryEntry(History history) {
		if (this.history == null)
			this.history = new ArrayList<History>();
		this.history.add(history);
	}

	@Override
	public Status getStatus() {
		return this.status;
	}

	@Override
	public Task getParent() {
		return this.parent;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((list == null) ? 0 : list.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + (int) (number ^ (number >>> 32));
		result = prime * result + ((parent == null) ? 0 : parent.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SimpleTaskBean other = (SimpleTaskBean) obj;

		if (number != 0 && number == other.number)
			return true;

		if (list == null) {
			if (other.list != null)
				return false;

		} else if (!list.equals(other.list))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (parent == null) {
			if (other.parent != null)
				return false;
		} else if (!parent.equals(other.parent))
			return false;
		return true;
	}

}
